let db = require('../db/index')

exports.login = (req,res) =>{//登录验证
    var sql = 'select * from users where name = ? and password = ?'
    db.query(sql,[req.query.name,req.query.password],(err,data) =>{
        if(err){
            return res.send({
                status:400,
                message: '登录失败'
            })
        }
        if(data.length>0){
            res.send({
                status:200,
                message:'登录成功'
            })
        }else{
            res.send({
                status:202,
                message:'用户名或密码错误'
            })
        }
    })
}

exports.register = (req,res) =>{//登录验证
    var sql1 = 'select * from users where name = ?'
    var sql2 = 'insert into users (name,password,id,sex,email) values(?,?,?,?,?)'
    db.query(sql1,[req.body.params.name],(err,data) =>{
        if(err){
            return res.send({
                status:400,
                message: '操作失败'
            })
        }
        if(data.length>0){
            return res.send({
                status:202,
                message:'用户名已存在'
            })
        }else{
            db.query(sql2,[req.body.params.name,req.body.params.password,req.body.params.id,req.body.params.sex,req.body.params.email],(err,data)=>{
                if(err){
                    return res.send({
                        status:400,
                        message:'注册失败'
                    })
                }
                res.send({
                    status:200,
                    message:'注册成功'
                })
            })
        }
    })
}